package com.quan.system.commons.utils;

import com.quan.system.entity.SysUser;
import lombok.extern.slf4j.Slf4j;

/**
 * 保存用户信息在ThreadLocal
 */
@Slf4j
public class LoginUser {

    private static ThreadLocal<SysUser> threadLocal = new ThreadLocal<>();

    /**
     * 设置用户信息
     * @param user
     */
    public static void setUser(SysUser user){
        log.info("当前线程 --- [{}] --- 设置用户 {} ", Thread.currentThread().getName(), user);
        threadLocal.set(user);
    }

    /**
     * 获取用户信息
     * @return
     */
    public static SysUser getUser() {
        SysUser user = threadLocal.get();
        log.info("当前线程 --- [{}] --- 获取用户 {} ", Thread.currentThread().getName(), user);
        return user;
    }

    /**
     * 移除用户
     */
    public static void removeUser() {
        threadLocal.remove();
    }
}
